package com.project.personalfundmanager.client.taxitems;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.project.personalfundmanager.client.common.Const;
import com.project.personalfundmanager.shared.models.Section10Model;
import com.project.personalfundmanager.shared.models.StringWrapperModel;
import com.project.personalfundmanager.shared.models.TaxableIncomeModel;

public class Section10Item extends ParentItem<Section10Model>
{
	private TaxableIncomeModel taxableIncomeModel;

	public Section10Item( Section10Model model, TaxableIncomeModel taxableIncomeModel )
	{
		super( model );
		this.taxableIncomeModel = taxableIncomeModel;
	}

	@Override
	public void addFormComponents( FormPanel formPanel, boolean isEditable )
	{
		addToPanel( formPanel, uiHelper.getNumberField( "LTA", Const.LTA, isEditable ) );
		addToPanel( formPanel, uiHelper.getNumberField( "House Rent Paid", Const.RENT_PAID, isEditable ) );
		addToPanel( formPanel, uiHelper.getNumberField( "Madical Reimbursement", Const.MEDICAL, isEditable ) );
		List<StringWrapperModel> typeOfCities = new ArrayList<StringWrapperModel>();
		typeOfCities.add( new StringWrapperModel("Metro") );
		typeOfCities.add( new StringWrapperModel("Non Metro") );
		addToPanel( formPanel, uiHelper.getCombobox( typeOfCities, Const.TYPE_OF_CITY, "Type of City", isEditable ) );
	}

	@Override
	public void addTotalComponents( FormPanel totalPanel )
	{
		addToPanel( totalPanel, uiHelper.getNumberField( "Calculated HRA", Const.CALCULATED_HRA, false ) );
		addToPanel( totalPanel, uiHelper.getNumberField( "Total",Const.SEC10_TOTAL , false ) );
	}

	public void calculateTotal()
	{
		double accessValue = model.getRentPaid() - taxableIncomeModel.getBasicSalary() * 0.1;
		double fourtyPercentOfBasic = taxableIncomeModel.getBasicSalary() * ( model.getTypeofCity().equals( "Metro" ) ? 0.5 : 0.4 );
		double hraReceived = taxableIncomeModel.getHRA().doubleValue();

		
		int hraCalculated = ( int ) Math.min(Math.min( accessValue, fourtyPercentOfBasic ),hraReceived);

		if ( hraCalculated >= 0 )
			model.setCalculatedHRA( hraCalculated );

		model.setTotal( model.getLTA().intValue() + model.getCalculatedHRA().intValue() + model.getMedical().intValue() );
	}
}
